package com.jsnkj.basic.activity

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.fragment.app.Fragment
import com.jsnkj.basic.R
import com.jsnkj.basic.advertisement.AdvertisementManager
import com.jsnkj.basic.config.RewardsConfig
import com.jsnkj.basic.databinding.ActivityReceiptsDetailBinding
import com.jsnkj.basic.fragment.IncomeFragment
import com.jsnkj.basic.fragment.WithdrawFragment
import com.jsnkj.basic.mvvm.BaseKtActivity
import com.jsnkj.basic.mvvm.HttpViewModel
import com.jsnkj.basic.util.RewardsUtil
import com.hzhj.openads.domain.HJAdError
import com.hzhj.openads.listener.HJOnAdsSdkBannerListener

//import com.tb.mob.TbManager
//import com.tb.mob.bean.Position


/**
 * 收支明细
 */
class ReceiptsDetailActivity : BaseKtActivity<ActivityReceiptsDetailBinding, HttpViewModel>() {
    override val layoutId: Int
        get() = R.layout.activity_receipts_detail


    companion object {
        fun goToReceiptsDetail(activity: Activity, viewPageIndex: Int) {
            val intent = Intent(activity, ReceiptsDetailActivity::class.java)
            intent.putExtra("viewPageIndex", viewPageIndex)
            activity.startActivity(intent)
        }
    }

    var tabTitle = arrayListOf("收入", "提现")
    val listFragment: MutableList<Fragment> = ArrayList()


    private val bannerHandler = Handler(Looper.getMainLooper())
    private var bannerRunnable: Runnable? = null

    override fun initView(savedInstanceState: Bundle?) {

        mVDB.topTitle.txtTopTitle.text = "收支明细"
        mVDB.topTitle.imageReturn.setOnClickListener {
            onBackPressed()
        }
        mVDB.topTitle.layoutTopTitle.setBackgroundColor(Color.parseColor("#FFFFFF"))


        listFragment.add(IncomeFragment.newInstance())
        listFragment.add(WithdrawFragment.newInstance())

        val viewPagerAdapter =
            com.jsnkj.basic.adapter.ReceiptsAdapter(
                supportFragmentManager, listFragment, tabTitle
            )
        mVDB.viewPager.setAdapter(viewPagerAdapter)
        mVDB.tabLayout.setupWithViewPager(mVDB.viewPager)


        val viewPageIndex = intent.getIntExtra("viewPageIndex", 0)
        mVDB.viewPager.setCurrentItem(viewPageIndex)


        // 初始化 Runnable 对象
        bannerRunnable = Runnable { // 这里写定时器需要执行的任务
            initBannerAd()
        }
        // 开始定时器
        bannerHandler.postDelayed(bannerRunnable!!, 3000)
    }


    private fun initBannerAd() {
        AdvertisementManager.loadHjBannerAds(this@ReceiptsDetailActivity,object : HJOnAdsSdkBannerListener{
            override fun onAdLoadSuccess(p0: String?) {
                AdvertisementManager.showHJBannerAds( mVDB.flContainer)
            }

            override fun onAdLoadError(p0: HJAdError?, p1: String?) {
                com.jsnkj.basic.util.LogUtils.e("启动banner广告 加载失败：$p1"+","+p0?.msg)
            }

            override fun onAdShown() {

            }

            override fun onAdClicked() {
                RewardsUtil.addGoldNumber(AdvertisementManager.getAdSettingListBean(RewardsConfig.adBanner)!!.reward_num as Int, RewardsConfig.adBanner)
            }

            override fun onAdClosed() {

            }

            override fun onAdAutoRefreshed() {

            }

            override fun onAdAutoRefreshFail(p0: HJAdError?, p1: String?) {
                com.jsnkj.basic.util.LogUtils.e("启动banner广告 自动刷新失败：$p1"+p0!!.msg)
            }

        })
//        AdvertisementManager.loadBanner(
//            this@ReceiptsDetailActivity,
//            SharePreferencesUtil.getInstance().getInt("app_screenWidth", 350) - 40,
//            mVDB.flContainer,
//            object : TbManager.BannerLoadListener {
//                override fun onFail(s: String) {
//                    BurialPointUtil.bannerAdEvent("fail")
//                    LogUtils.e("banner广告 加载失败：$s")
//                    mVDB.flContainer.visibility = View.INVISIBLE
//                }
//
//                override fun onDismiss() {
//                    mVDB.flContainer.visibility = View.INVISIBLE
//                }
//
//                override fun onClicked() = Unit
//                override fun onExposure(position: Position) {
//                    BurialPointUtil.bannerAdEvent("show")
//                    RewardsUtil.addGoldNumber(position.ecpm, RewardsConfig.adBanner)
//                }
//            })
    }


    override fun onPause() {
        super.onPause()
        // 取消定时器，避免内存泄漏
        bannerHandler.removeCallbacks(bannerRunnable!!)
    }

}